#!/usr/bin/env bash


function parseArgs()
{
  function init_deploy()
  {
    local -r flag=$1
    for c in ${COMPONENTS[@]}; do
      MAP_DEPLOY[${c}]="${flag}"
    done
  }
  function init_undeploy()
  {
    local -r flag=$1
    for c in ${COMPONENTS[@]}; do
      MAP_UNDEPLOY[${c}]="${flag}"
    done
  }
  function showUsage()
  {
    echo "Usage: deploy-k8s.sh <command> [components...]"
    echo "  Deploy or undeploy kubernetes components and addons, all or specified."
    echo ""
    echo "       command, only one of below:"
    echo "          -a, --all     : Deploy all components, execute clear before."
    echo "          -c,--clean    : Undeploy (clean) all components, including pacakges, files, addons..."
    echo "          -i,--deploy   : Deploy components specified follow, must specify one or more."
    echo "          -u,--undeploy : Undeploy components specified follow, must specify one or more."
    echo ""
    echo -n "       components defined in sequence: "
    local -i COUNT=0
    for c in ${COMPONENTS[@]}; do
      [ ${COUNT} -gt 0 ] && echo -n ", "     
      (( COUNT++ ))

      echo -n "${c}"
    done
    echo ""
    echo ""
  }

  local -r OPT_ARGS=$(getopt -o :aciu --long all,clean,deploy,undeploy -- "$@")
  eval set -- ${OPT_ARGS}

  local FLAG="ERROR" # CONTINUE | BREAK | ERROR
  local CMD="UNSET" # UNSET | ALL | DEPLOY | UNDEPLOY | CLEAN
  init_deploy false
  init_undeploy false
  while [ -n "$1" ]; do
    case "$1" in
      -a|--all)
        if [ ${CMD} == "UNSET" ]; then
          CMD="ALL"
          FLAG="CONTINUE"
          init_deploy true
        fi
        ;;
      -c|--clean)
        if [ ${CMD} == "UNSET" ] ; then
          CMD="CLEAN"
          FLAG="CONTINUE"
        fi
        ;;
      -i|--deploy)
        if [ ${CMD} == "UNSET" ]; then
          CMD="DEPLOY"
          FLAG="CONTINUE"
        fi
        ;;
      -u|--undeploy)
        if [ ${CMD} == "UNSET" ]; then
          CMD="UNDEPLOY"
          FLAG="CONTINUE"
        fi
        ;;
      --)
        ;;
      *)
        if [[ -n "${MAP_DEPLOY[$1]}" && "${CMD}" == "DEPLOY" ]]; then
          MAP_DEPLOY[$1]="true"
          FLAG="CONTINUE"
        elif [[ -n "${MAP_UNDEPLOY[$1]}" && "${CMD}" == "UNDEPLOY" ]]; then
          MAP_UNDEPLOY[$1]="true"
          FLAG="CONTINUE"
        else
          FLAG="ERROR"
        fi
    esac

    # FLAG == "continue"
    [ ${FLAG} == "ERROR" ] && break

    shift
  done

  [ ${FLAG} == "ERROR" ] && showUsage && exit 1
}

#----------------------------
#set -x
# define all components default in sequnce
declare -ar COMPONENTS=(ca etcd ha k8s apiserver controller scheduler kubelet proxy calico coredns metrics ingress dashboard prometheus rookceph grafana efk mysql mysqlcluster)
declare -A MAP_DEPLOY
declare -A MAP_UNDEPLOY
declare FLAG_CLEAN="false"
declare FLAG_INIT="true"

parseArgs $@

for c in ${COMPONENTS[@]}; do
  if [ "${MAP_DEPLOY[$c]}" == "true" ]; then
    eval "deploy_$c"
  fi
done

echo "Undeploy................"
declare comp
for (( i=${#COMPONENTS[@]}-1; i>=0; i-- )); do
  comp=${COMPONENTS[$i]}
  if [ "${MAP_UNDEPLOY[${comp}]}" == "true" ]; then
    eval "undeploy_${comp}"
  fi
done
